(function () {
    var domkeys = ['d', 'h', 'm', 's'];
    var slice = Array.prototype.slice;
    var query = document.querySelector.bind(document);
    var createEl = document.createElement.bind(document);
    function Timer(rooms, li) {
        this.li = li;
        this.time = null;
        this.timer = null;
        this.shellId = 'shell' + new Date().getTime();
        domkeys.forEach(function (name) {
            this[name] = this.getDom(rooms[name]);
        }, this);
    }
    Timer.prototype = {
        constructor: Timer,
        // 开始倒计时
        start: function (time, callback) {
            this.time = time;
            this.timer = setInterval(function () {
                var time = this.time;
                if (time < 1000) {
                    clearInterval(this.timer);
                    if (callback) callback();
                    return;
                }
                this.time = time - 1000;
                this.updateTime();
            }.bind(this), 1000);
            this.updateTime();
            return this;
        },
        // 停止倒计时
        stop: function (callback) {
            clearInterval(this.timer);
            if (callback) callback();
            return this;
        },
        // 继续倒计时
        go: function (callback) {
            this.start(this.time);
            if (callback) callback();
            return this;
        },
        // 更新时间
        updateTime: function () {
            var time = this.time,
                d1 = 24 * 60 * 60 * 1000,
                h1 = 60 * 60 * 1000,
                m1 = 60 * 1000,
                s1 = 1000,
                d, h, m, s;
            if (this.d && this.d.id !== this.shellId) {
                d = parseInt(time / d1);
                time = time % d1;
            }
            if (this.h && this.h.id !== this.shellId) {
                h = parseInt(time / h1);
                time = time % h1;
            }
            if (this.m && this.m.id !== this.shellId) {
                m = parseInt(time / m1);
                time = time % m1;
            }
            if (this.s && this.s.id !== this.shellId) {
                s = parseInt(time / s1);
                this.updateNode(d, h, m, s);
            }
            console.log(d + "天" + h + "时" + m + "分" + s + "秒");
        },
        // 更新节点
        updateNode: function (d, h, m, s) {
            var times = slice.apply(arguments),
                obj = { 'd': '', 'h': '', 'm': '', 's': '' };
            times.forEach(function (time, i) {
                var name = domkeys[i],
                    strs = this.transformTime(time);
                strs.split("").forEach(function (str) {
                    obj[name] += this.transformDom(str);
                }, this)
            }, this);
            this.renderNode(obj.d, obj.h, obj.m, obj.s);
        },
        // 渲染节点
        renderNode: function (d, h, m, s) {
            this.d.innerHTML = d;
            this.h.innerHTML = h;
            this.m.innerHTML = m;
            this.s.innerHTML = s;
        },
        // 转换dom
        transformDom: function (str) {
            var li = this.li;
            var reg = /(^\<[^>]+\>)(.*)(\<[^>]+\>)$/;
            return li ? li.replace(reg, '$1' + str + '$3') : str;
        },
        // 转换时间
        transformTime: function (num) {
            if (num < 10) {
                return '0' + num;
            } else {
                return String(num);
            }
        },
        // 创建空容器
        createShell: function (is) {
            var dom = createEl('div');
            if (is) dom.id = this.shellId;
            return dom;
        },
        // 是否为dom元素
        isDom: (function (is) {
            if (is) {
                return function (obj) {
                    return obj instanceof HTMLElement;
                }
            } else {
                return function (obj) {
                    return obj && typeof obj === 'object' && obj.nodeType === 1 && typeof obj.nodeName === 'string';
                }
            }
        }(typeof HTMLElement === 'object')),
        // 获得dom元素
        getDom: function (obj) {
            if (typeof obj === '') {
                return this.createShell(false);
            }
            if (typeof obj === 'undefined') {
                return this.createShell(true);
            }
            if (typeof obj === 'string') {
                return query(obj);
            }
            if (this.isDom(obj)) {
                return obj;
            }
        }
    }
    window.Timer = Timer;
}());